//
//  TbInfoView.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2020/2/6.
//  Copyright © 2020 zwb. All rights reserved.
//  通用内容动态行数展示框

import UIKit

class ZyaTbInfoView: UIView {
    
    @IBOutlet weak var titleLab: UITextField!
    @IBOutlet weak var wenhao: UIButton!
    @IBOutlet weak var rankTip: UILabel!
    @IBOutlet private weak var content1: CJLabel!
    var contentStr:String = "" {
        didSet {
            let paraph = NSMutableParagraphStyle()
            //将行间距设置为28
            paraph.lineSpacing = 5
//            //样式属性集合
//            let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
//                              NSAttributedString.Key.paragraphStyle: paraph]
//            self.content1.attributedText = NSAttributedString(string: contentStr, attributes: attributes)
//            
          
              
      
        
            // 假设使用相同的字体来计算空格宽度
            let spaceFont = content1.font
            let spaceString = " "
            let spaceAttributes = [NSAttributedString.Key.font: spaceFont]
            let spaceWidth = spaceString.size(withAttributes: spaceAttributes).width
      
            // 分割文本并处理每对键值
            let parts = contentStr.components(separatedBy: "#")
            let attributedString = NSMutableAttributedString()
      
            var maxKeyLength: CGFloat = 0
            //提前算出最大左边宽度
            for part in parts {
                guard !part.isEmpty else { continue }
                let keyValue = part.components(separatedBy: ":")
                guard keyValue.count >= 2 else { continue }
      
                let key = keyValue[0].trimmingCharacters(in: .whitespacesAndNewlines)
                let keyAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14)]
                let keySize = key.size(withAttributes: keyAttributes)
                maxKeyLength = max(maxKeyLength, keySize.width)
            }
      
            for part in parts {
                guard !part.isEmpty else { continue }
                let keyValue = part.components(separatedBy: ":")
                guard keyValue.count >= 2 else { continue }
      
                let key = keyValue[0].trimmingCharacters(in: .whitespacesAndNewlines)
                let keyAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14),NSAttributedString.Key.paragraphStyle: paraph]
                let keySize = key.size(withAttributes: keyAttributes)
              
      
                var value = keyValue[1].trimmingCharacters(in: .whitespacesAndNewlines)
                if value == "" {
                    value = "-"
                }
      
                // 计算空格数
                let spacesCount = Int((maxKeyLength - keySize.width) / spaceWidth)
                let spacesString = String(repeating: " ", count: spacesCount)
      
                let fullLine = NSMutableAttributedString()
                // 把冒号换成空格，有点距离，又符合需求
                fullLine.append(NSAttributedString(string: key + " ", attributes: keyAttributes))
                fullLine.append(NSAttributedString(string: spacesString + value + "\n", attributes: keyAttributes))
      
                attributedString.append(fullLine)
            }
       
      
 
            content1.attributedText = attributedString
            
             
        }
    }
    
    var attributedContentStr:NSAttributedString? {
        didSet {
            if let aContent = attributedContentStr {
                self.content1.attributedText = aContent
            }
           
            
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        content1.enableCopy = true
        content1.font = UIFont.systemFont(ofSize: 14)
        content1.numberOfLines = 0
        content1.textAlignment = .left
        content1.preferredMaxLayoutWidth = KSGlobal.screenW - 32
    }
    
    
}
 
 
